#!/bin/sh

handle_link () {
    link=$1
    domain="${link#https://}"
    domain="${domain%%/*}"
    case "$domain" in
	#invidious list found here: https://docs.invidious.io/instances
	*youtube*|*invidious*|vid.puffyan.us|yewtu.be|inv.riverside.rocks|yt.artemislena.eu|tube.cthd.icu)
		link=$(_get_real_channel_link "$1")
		final="https://www.youtube.com/feeds/videos.xml?channel_id=${link##*/}"
		;;
	*reddit*) final="$link/.rss" ;;
    esac
}

scrape_feed_url () {
    IFS=" "
    if [ "$1" = ":help" ]; then
	printf "%s\n" "Get the rss feed for something
supported searches:
    youtube/<channel-name>
    <link-to-youtube-channel>
    r/subreddit
    <link-to-subreddit>
example searches:
    youtube/pewdiepie
    r/linux"
	return 100
    fi
    for link in $1; do
	case "$link" in
	    r/*) final="https://www.reddit.com/r/${link#r/}/.rss" ;;
	    youtube/*)
		link="https://www.youtube.com/user/${link#youtube/}"
		handle_link "$link"
		;;
	    *)
		handle_link "$link" ;;
	esac
	#i honestly don't even know how it's possible that non-printable characters end up here
	final="$(printf "%s" "$final" | sed 's/[^[:print:]]//g')"
	printf "%s\n" "$final"
	_get_request "$final" -L --head --silent -f > /dev/null|| print_warning "Warning: $final, does not appear to be a real url\n"
    done
    exit 0
}
